package com.palm.easy.controller;

import com.palm.easy.anno.Auth;
import com.palm.easy.domain.FileData;
import com.palm.easy.domain.LogPart;
import com.palm.easy.domain.Service;
import com.palm.easy.service.ConfigService;
import com.palm.easy.service.Server;
import com.palm.easy.service.ServerManageService;
import com.palm.easy.util.StringUtil;
import org.noear.solon.Utils;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Result;
import org.noear.solon.core.handle.UploadedFile;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

@Auth
@Controller
@Mapping("server")
public class ServerController {
    @Inject
    ServerManageService manageService;
    @Inject
    ConfigService configService;


    @Mapping("names")
    public Result<Collection<String>> serverNames() {
        return Result.succeed(manageService.serverNames());
    }

    @Get
    @Mapping("list_services")
    public Result<List<Service>> listServices() {
        return Result.succeed(manageService.listServices());
    }

    @Get
    @Mapping("list_servers")
    public Result<Set<String>> listServers() {
        return Result.succeed(manageService.serverNames());
    }

    @Post
    @Mapping("add_service")
    public Result addService(Service service) {
        if (service == null) {
            return Result.failure("services不能为空");
        }
        if (StringUtil.isEmpty(service.getServer())) {
            return Result.failure("server不能为空");
        }
        Server server = manageService.getServer(service.getServer());
        if (server == null) {
            return Result.failure("server不存在");
        }
        server.addService(service);
        return Result.succeed();
    }

    @Post
    @Mapping("save_service")
    public Result saveService(Service service) {
        if (service == null) {
            return Result.failure("services不能为空");
        }
        if (StringUtil.isEmpty(service.getServer())) {
            return Result.failure("server不能为空");
        }
        Server server = manageService.getServer(service.getServer());
        if (server == null) {
            return Result.failure("server不存在");
        }
        server.saveService(service);
        return Result.succeed();
    }

    @Post
    @Mapping("del_service")
    public Result delService(Service service) {
        if (service == null) {
            return Result.failure("services不能为空");
        }
        if (StringUtil.isEmpty(service.getServer())) {
            return Result.failure("server不能为空");
        }
        manageService.delService(service);
        return Result.succeed();
    }

    @Post
    @Mapping("service_upload")
    public Result service_upload(Service service, Context ctx) throws Exception {
        Server server = manageService.getServer(service.getServer());
        if (server == null) {
            return Result.failure("未找到服务器");
        }
        UploadedFile file = ctx.file("file");
        if (file == null) {
            return Result.failure("未上传文件");
        }

        byte[] data = Utils.transferToBytes(file.content);
        file.content.close();
        server.uploadFile(service,configService.getConfigs(), data);
        return Result.succeed();
    }

    @Post
    @Mapping("start_service")
    public Result start(Service service) {
        Server server = manageService.getServer(service.getServer());
        if (server == null) {
            return Result.failure("未找到服务器");
        }
        server.startService(service,configService.getConfigs());
        return Result.succeed();
    }

    @Post
    @Mapping("stop_service")
    public Result stop(Service service) {
        Server server = manageService.getServer(service.getServer());
        if (server == null) {
            return Result.failure("未找到服务器");
        }
        server.stopService(service);
        return Result.succeed();
    }

    @Get
    @Mapping("get_log")
    public Result<LogPart> getLog(String service,String server, long pos) {
        Server theServer = manageService.getServer(server);
        if (theServer == null) {
            return Result.failure("未找到服务器");
        }
        LogPart part = theServer.getLog(service, pos);
        return Result.succeed(part);
    }

    @Mapping("list_file")
    public Result listFile(String serverName, String path) {
        Server server = manageService.getServer(serverName);
        if (server == null) {
            return Result.failure("未找到服务器");
        }
        return Result.succeed(server.fileList(path));
    }

    @Mapping("remove_file")
    public Result removeFile(String serverName, String path) {
        Server server = manageService.getServer(serverName);
        if (server == null) {
            return Result.failure("未找到服务器");
        }
        if (server.removeFile(path)) {
            return Result.succeed("已删除");
        }
        return Result.failure("删除失败");
    }
    @Mapping("rename")
    public Result rename(String serverName, String path,String newName) {
        Server server = manageService.getServer(serverName);
        if (server == null) {
            return Result.failure("未找到服务器");
        }
        if (server.reName(path,newName)) {
            return Result.succeed("已重命名");
        }
        return Result.failure("文件重命名失败");
    }
    @Mapping("export_file")
    public Result exportFile(String serverName, String path, Context ctx) {
        Server server = manageService.getServer(serverName);
        if (server == null) {
            return Result.failure("未找到服务器");
        }
        FileData data = server.exportFile(path);
        if (data == null) {
            return Result.failure("导出失败");
        }
        ctx.setHandled(true);
        ctx.headerAdd("Content-Type", "application/download");
        try {
            ctx.headerAdd("Content-Disposition", "attachment;filename=" + URLEncoder.encode(data.getName(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        ctx.output(data.getData());
        return null;
    }

    @Mapping("upload_file")
    public Result upload(String serverName, String path, Context ctx) throws Exception {
        Server server = manageService.getServer(serverName);
        if (server == null) {
            return Result.failure("未找到服务器");
        }
        UploadedFile uf = ctx.file("file");
        if (uf == null) {
           return Result.failure("上传文件失败");
        }
        FileData fd = new FileData();
        fd.setName(uf.name);
        fd.setData(Utils.transferToBytes(uf.content));
        server.uploadServerFile(path, fd);
        return Result.succeed("上传完成");
    }

    @Mapping("unzip_file")
    public Result unZip(String serverName, String path) {
        Server server = manageService.getServer(serverName);
        if (server == null) {
            return Result.failure("未找到服务器");
        }
        return server.unZipFile(path) ? Result.succeed("已解压") : Result.failure("解压失败");
    }

    @Mapping("load_file")
    public Result loadFile(String serverName, String path) {
        Server server = manageService.getServer(serverName);
        if (server == null) {
            return Result.failure("未找到服务器");
        }
        return Result.succeed(server.loadFile(path));
    }

    @Mapping("save_file")
    public Result saveFile(String serverName, String path, String file) {
        Server server = manageService.getServer(serverName);
        if (server == null) {
            return Result.failure("未找到服务器");
        }
        server.saveFile(path, file);
        return Result.succeed();
    }
    @Mapping("new_file")
    public Result newFile(String serverName, String path, String file){
        Server server = manageService.getServer(serverName);
        if (server == null) {
            return Result.failure("未找到服务器");
        }
        server.newFile(path, file);
        return Result.succeed();
    }
    @Mapping("new_dir")
    public Result newDir(String serverName, String path, String file){
        Server server = manageService.getServer(serverName);
        if (server == null) {
            return Result.failure("未找到服务器");
        }
        server.newDir(path, file);
        return Result.succeed();
    }
    @Mapping("status")
    public Result status(String server){
        Server the_server = manageService.getServer(server);
        if (the_server == null) {
            return Result.failure("未找到服务器");
        }
        Map<String,Object> status=the_server.status();

        return Result.succeed(status);
    }
    @Mapping("sevice_status")
    public Result serviceStatus(Service service){
        Server the_server = manageService.getServer(service.getServer());
        if (the_server == null) {
            return Result.failure("未找到服务器");
        }
        Map<String,Object> status=the_server.serviceStatus(service);
        if(status==null){
            return Result.failure("未获取到状态");
        }
        return Result.succeed(status);
    }
}
